package com.kx.smsplantform.strategy.impl;

import com.kx.smsplantform.client.CacheClient;
import com.kx.smsplantform.strategy.Strategy;
import com.kx.smsplantform.utils.PushMessage2Queue;
import com.kx.smsplatform.common.constants.CacheConstants;
import com.kx.smsplatform.common.constants.StrategyConstants;
import com.kx.smsplatform.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Set;

/**
 * 一天限流处理策略
 *
 * @Author Sun
 * @Date 2021/7/2 11:36
 */
@Slf4j
@Service(value = "limitOneDay")
public class LimitOneDayStrategy implements Strategy {
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private PushMessage2Queue pushMessage2Queue;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("【策略模块-一天限流校验】 一天限流校验策略开始执行！！");
        int clientID = standardSubmit.getClientID();
        String destMobile = standardSubmit.getDestMobile();
        String key = CacheConstants.CACHE_PREFIX_SMS_LIMIT + clientID + destMobile;
        Set limitSet =cacheClient.limit(key,new Date().getTime()-1000*60*60*24,new Date().getTime());
        if(limitSet.size()>=30){
            log.info("【策略模块-五分钟限流校验】短信条数已经超过一天承受的最大条数");
            pushMessage2Queue.sendLogMessage(standardSubmit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMessage2Queue.pushReportMessage(standardSubmit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            return false;
        }

        log.info("【策略模块-一天限流校验】 一天限流校验策略执行结束！！");
        return true;
    }
}
